import { Injectable } from '@nestjs/common';
import { exec } from 'child_process';
import * as fs from 'fs';
@Injectable()
export class UserService {
  async executeLs() {
    return new Promise((resolve, reject) => {
      exec(
        'C:\\Users\\admin\\Desktop\\dftZhuanhuanqi\\draftingee-heconverter202406011_v2.5\\DftConverterHE.exe -i C:\\Users\\admin\\Desktop\\004turbofan/1_etapa_compresor_estatores.CATPart -o C:\\Users\\admin\\Desktop\\top.pdv -renderbuffer 1',
        { encoding: 'utf8' },
        (error, stdout, stderr) => {
          if (error) {
            reject(error);
          } else {
            resolve(stdout);
          }
        },
      );
    });
  }

  async convertFile(inputPath: string, outputPath: string): Promise<void> {
    try {
      const command = `C:\\Users\\admin\\Desktop\\dftZhuanhuanqi\\draftingee-heconverter202406011_v2.5\\DftConverterHE.exe -i ${inputPath} -o ${outputPath} -renderbuffer 1`;
      const { stdout, stderr } = await exec(command);
    } catch (error) {
      console.error('Error executing command:', error);
    }
  }

  async getFilesInFolder(folderPath: string): Promise<string[]> {
    try {
      const files = await fs.promises.readdir(folderPath);
      //   return files.map((file) => `${folderPath}/${file}`);
      const arr = folderPath.split('\\');
      files.filter((file) => {
        this.convertFile(
          `${folderPath}/${file}`,
          `${folderPath}/${'copy' + arr[arr.length - 1]}/${file}`,
        );
      });
      return ['200'];
    } catch (error) {
      console.error('Error reading directory:', error);
      throw error; // 抛出错误以便处理
    }
  }
}
